#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef union levle
{
  int fenshu;
  float lv;
}LEVEL;
typedef struct stu
{
  char stuid[10];
  char stuname[10];
  int stuage;
  char stusex;
  int achievement[6];
  float ave;
  float sum;
  char classn[10];
}STUDENT;
typedef struct class
{
  char classname[10];
  STUDENT student[40];
  int count;
  LEVEL ab[6][5];
}CLASS;
CLASS* creat_table(STUDENT *prept,int *n,int *k);
void check_classnum_subject(int *n,int *k);
int check_stu_num(void);
int check_add_stu(int *n);
void addstu(CLASS *p,int *n,int *k);
int check_wei(CLASS *p,int j);
void delatestu(CLASS *p,int *n,int *k);
int check_del_class(int *n);
int check_del_wei(CLASS *p,int j);
int check_erji(void);
void inquire_class(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5]);
int inquiry_class_check(int *n);
int select_inquiry_caidan(void);
void inquire_name(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5]);
void inquire_stuid(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5]);
void select_caidan(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5]);
void inquire(CLASS *p,STUDENT  *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5]);
void calculate_ave_sum(CLASS *p,int *n,int *k);
void addname(CLASS *p,int *n);
STUDENT * paixu_as_z(CLASS *p,int *n,int *k,STUDENT *prept,STUDENT *pt);
void paixu(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5]);
void printstu(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5]);
void calculate_zhibiao(CLASS *p,STUDENT *pt,int *n,int *k,LEVEL nz[6][5]);
int main()
{
  CLASS *stu;
  STUDENT *prept;
  if((prept=(STUDENT *)malloc(sizeof(STUDENT)))==NULL)
  {
    printf("在申请补丁内存时候出现错误!\n");
    exit(-3);
  }
  STUDENT *pt=prept;
  int n=0,k=0;
  LEVEL nz[6][5];
  printf("欢迎您进入我的学生管理系统!\n");
  printf("创建学生信息表\n");
  stu=creat_table(prept,&n,&k);
  select_caidan(stu,prept,pt,&n,&k,nz);
  return 0;
}
void select_caidan(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5])
{
  int flag;
  while(1)
  {
    printf("1.添加学生信息\n2.删除学生信息\n3.计算学生的平均分和总分\n4.查询(包括按班级,姓名,学号,性别)\n5.排序(包括按平均分,总分以及姓名排序)\n6.写入文件进行可视化\n7.退出系统\n8.计算每科的指标\n");
    printf("请输入你的选择:");
    if(scanf("%d",&flag)!=1)
    {
      printf("请输入数字\n");
      while(getchar()!='\n');
      select_caidan(p,prept,pt,n,k,nz);
    }
    else if(flag<1||flag>8)
    {
      printf("无效功能\n");
      select_caidan(p,prept,pt,n,k,nz);
    }
    else
    break;
  }
  switch(flag)
  {
    case 1:
    {
      addstu(p,n,k);
      select_caidan(p,prept,pt,n,k,nz);
    }
    case 2:
    {
      delatestu(p,n,k);
      select_caidan(p,prept,pt,n,k,nz);
    }
    case 3:
    {
      calculate_ave_sum(p,n,k);
      select_caidan(p,prept,pt,n,k,nz);
    }
    case 4:
    {
      inquire(p,prept,pt,n,k,nz);
    }
    case 5:
    {
      paixu(p,prept,pt,n,k,nz);
    }
    case 6:
    {
    printstu(p,prept,pt,n,k,nz);
    select_caidan(p,prept,pt,n,k,nz);
    }
    case 7:
    {
      free(p);
      if(pt==prept)
      {
        free(prept);
      }
      else
      {
        free(prept);
        free(pt);
      }
      exit(0);
    }
    case 8:
    {
      calculate_zhibiao(p,pt,n,k,nz);
      select_caidan(p,prept,pt,n,k,nz);
    }

  }
}

CLASS* creat_table(STUDENT *prept,int *n,int *k)
{
  int i,j,a;
  CLASS *p;
  check_classnum_subject(n,k);
  if((p=(CLASS*)malloc((*n)*sizeof(CLASS)))==NULL)
  {
    printf("申请内存失败 !\n");
    free(prept);
    exit(-1);
  }
  else
  {
    for(i=0;i<(*n);i++)
    {
      printf("请输入第%d个班的班级名\n",i+1);
      scanf("%3s",(p+i)->classname);
      (*(p+i)).count=check_stu_num();
      for(j=0;j<(p+i)->count;j++)
      {
        printf("请输入第%d位学生的学号,姓名,年龄,性别",j+1);
        scanf("%3s%3s%d %c",(p+i)->student[j].stuid,(p+i)->student[j].stuname,&((p+i)->student[j].stuage),&((p+i)->student[j].stusex));
        for(a=0;a<(*k);a++)
        {
          printf("请输入第%d个课程的成绩\n",a+1);
          scanf("%d",&((p+i)->student[j].achievement[a]));
        }
      }
    }
  }
  printf("所有信息已录入到堆中\n");
  return p;
}

int check_stu_num(void)
{
  int i;
  while(1)
  {
    printf("请输入该班级的学生人数:\n");
    if(scanf("%d",&i)!=1)
    {
      printf("请输入数字懂吗\n");
      while(getchar()!='\n');
      continue;
    }
    else if((i<=0)||(i>40))
    {
      printf("学生人数超出限制\n");
      continue;
    }
    else
    printf("学生人数记录完毕");
    break;
  }
  return i;
}

void check_classnum_subject(int *n,int *k)
{
  while(1)
  {
    printf("请输入您所要记录的班级数和学科数:\n");
    if(scanf("%d%d",n,k)!=2)
    {
      printf("请输入数字!\n");
      while(getchar()!='\n');
      continue;
    }
    else if(((*n)<=0)||((*k)<=0))
    {
      printf("请不要输入负数或者0 !\n");
      continue;
    }
    else if((*k)>6)
    {
      printf("学科数超过上限,请重新输入!\n");
      continue;
    }
    else if((*n)>10)
    {
      printf("班级个数超出上限\n");
      continue;
    }
    else
    break;
  }
}

void addstu(CLASS *p,int *n,int *k)
{
  int i,j,a;
  for(i=0;i<(*n);i++)
  {
    printf("%d.%s\n",i+1,(p+i)->classname);
  }
  j=check_add_stu(n);
  a=check_wei(p,j);
  if(a==((p+j-1)->count)+1)
  {
    printf("请输入学生的学号,姓名,年龄,性别\n");
    scanf("%3s%3s%d %c",(p+j-1)->student[a-1].stuid,(p+j-1)->student[a-1].stuname,&((p+j-1)->student[a-1].stuage),&((p+j-1)->student[a-1].stusex));
    for(i=0;i<(*k);i++)
    {
      printf("请输入第%d个课程的成绩\n",i+1);
      scanf("%d",&(p+j-1)->student[a-1].achievement[i]);
    }
    ((p+j-1)->count)++;
  }
  else if(a==-1)
  {
    printf("学生已满\n");
  }
  else 
  {
    for(i=0;i<((p+j-1)->count)-a+1;i++)
    {
      (p+j-1)->student[((p+j-1)->count)-i]=(p+j-1)->student[((p+j-1)->count)-i-1];
    }
    printf("请输入学生的学号,姓名,年龄,性别\n");
    scanf("%3s%3s%d %c",(p+j-1)->student[a-1].stuid,(p+j-1)->student[a-1].stuname,&((p+j-1)->student[a-1].stuage),&((p+j-1)->student[a-1].stusex));
    for(i=0;i<(*k);i++)
    {
      printf("请输入第%d个课程的成绩\n",i+1);
      scanf("%d",&(p+j-1)->student[a-1].achievement[i]);
    }
    ((p+j-1)->count)++;
  }
}

int check_add_stu(int *n)
{
  int j;
  while(1)
  {
    printf("请输入你所要添加的班级:");
    if(scanf("%d",&j)!=1)
    {
      printf("请输入数字:\n");
      while(getchar()!='\n');
      continue;
    }
    else if(j<1||j>(*n))
    {
      printf("班级不存在\n");
      continue;
    }
    else
    break;
  }
  return j;
}

int check_wei(CLASS *p,int j)
{
  int a;
  if((p+j-1)->count<40)
  {
    while(1)
    {
      printf("请输入你所要添加的位置:");
      if(scanf("%d",&a)!=1)
      {
        printf("请输入数字:\n");
        while(getchar()!='\n');
        continue;
      }
      else if(a<1||a>((p+j-1)->count)+1)
      {
        printf("位置超出限制\n");
        continue;
      }
      else
      break;
    }
    return a;
  }
  else
  {
    return -1;
  }
}

void delatestu(CLASS *p,int *n,int *k)
{
  int i,j,a;
  for(i=0;i<(*n);i++)
  {
    printf("%d.%s\n",i+1,(p+i)->classname);
  }
  j=check_del_class(n);
  a=check_del_wei(p,j);
  if(a==(p+j-1)->count)
  {
    *((p+j-1)->student[a-1].stuid)='\0';
    *((p+j-1)->student[a-1].stuname)='\0';
    (p+j-1)->student[a-1].stusex='\0';
    (p+j-1)->student[a-1].stuage=0;
    for(i=0;i<(*k);i++)
    {
      (p+j-1)->student[a-1].achievement[i]=0;
    }
    ((p+j-1)->count)--;
  }
  else
  {
    for(i=((p+j-1)->count)-a;i>0;i--)
    {
      (p+j-1)->student[((p+j-1)->count)-i-1]=(p+j-1)->student[((p+j-1)->count)-i];
    }
    *((p+j-1)->student[((p+j-1)->count)-1].stuname)='\0';
    *((p+j-1)->student[((p+j-1)->count)-1].stuid)='\0';
    (p+j-1)->student[((p+j-1)->count)-1].stusex='\0';
    (p+j-1)->student[((p+j-1)->count)-1].stuage=0;
    for(i=0;i<(*k);i++)
    {
      (p+j-1)->student[((p+j-1)->count)-1].achievement[i]=0;
    }
    ((p+j-1)->count)--;
  }
}

int check_del_class(int *n)
{
  int j;
  while(1)
  {
    printf("请输入你所要删除的学生班级:");
    if(scanf("%d",&j)!=1)
    {
      printf("请输入数字:\n");
      while(getchar()!='\n');
      continue;
    }
    else if(j<1||j>(*n))
    {
      printf("班级不存在\n");
      continue;
    }
    else
    break;
  }
  return j;
}

int check_del_wei(CLASS *p,int j)
{
    int a;
    while(1)
    {
      printf("请输入你所要删除的学生位置:");
      if(scanf("%d",&a)!=1)
      {
        printf("请输入数字:\n");
        while(getchar()!='\n');
        continue;
      }
      else if(a<1||a>(p+j-1)->count)
      {
        printf("位置超出限制\n");
        continue;
      }
      else
      break;
    }
    return a;
}

void inquire(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5])
{
  int flag;
  printf("请你选择查询方式:\n");
  printf("1.按班级查询\n2.按姓名查询\n3.按学号查询\n");
  flag=check_erji();
  switch(flag)
  {
    case 1:
    inquire_class(p,prept,pt,n,k,nz);
    case 2:
    inquire_name(p,prept,pt,n,k,nz);
    case 3:
    inquire_stuid(p,prept,pt,n,k,nz);
  }
}

int check_erji(void)
{
  int flag;
  while(1)
  {
    if(scanf("%d",&flag)!=1)
    {
      printf("请输入数字\n");
      while(getchar()!='\n');
      continue;
    }
    else if(flag<1||flag>4)
    {
      printf("无效选项\n");
      continue;
    }
    else
    break;
  }
  printf("正在查询请稍后\n");
  return flag;
}

void inquire_class(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5])
{
  int i,j,a,flag;
  for(i=0;i<(*n);i++)
  {
    printf("%d.%s\n",i+1,(p+i)->classname);
  }
  j=inquiry_class_check(n);
  for(i=0;i<(p+j-1)->count;i++)
  {
    printf("%s %s %d %c ",(p+j-1)->student[i].stuid,(p+j-1)->student[i].stuname,(p+j-1)->student[i].stuage,(p+j-1)->student[i].stusex);
    for(a=0;a<(*k);a++)
    {
      printf("%d ",(p+j-1)->student[i].achievement[a]);
    }
    printf("%f %f\n",(p+j-1)->student[i].ave,(p+j-1)->student[i].sum);
  }
  flag=select_inquiry_caidan();
  switch(flag)
  {
    case 1:
    inquire_class(p,prept,pt,n,k,nz);
    case 2:
    inquire(p,prept,pt,n,k,nz);
    case 3:
    select_caidan(p,prept,pt,n,k,nz);
    case 4:
    {
      free(p);
      if(pt==prept)
      {
        free(prept);
      }
      else
      {
        free(prept);
        free(pt);
      }
      exit(0);
    }
  }
}

int inquiry_class_check(int *n)
{
  int j;
  while(1)
  {
    printf("请输入你所要查询的班级:");
    if(scanf("%d",&j)!=1)
    {
      printf("请输入数字:\n");
      while(getchar()!='\n');
      continue;
    }
    else if(j<1||j>(*n))
    {
      printf("班级不存在\n");
      continue;
    }
    else
    break;
  }
  return j;
}

void inquire_name(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5])
{
  int i,j,x,flag;
  for(i=0;i<(*n);i++)
  {
    for(j=0;j<(p+i)->count;j++)
    {
      printf("%s ",(p+i)->student[j].stuname);
    }
    printf("\n");
  }
  char a[10];
  printf("请输入你所要查询的姓名:");
  scanf("%3s",a);
  for(i=0;i<(*n);i++)
  {
    for(j=0;j<(p+i)->count;j++)
    {
      if(strcmp(a,(p+i)->student[j].stuname)==0)
      {
        printf("查询成功!\n");
        printf("%s %s %s %d %c ",(p+i)->classname,(p+i)->student[j].stuid,(p+i)->student[j].stuname,(p+i)->student[j].stuage,(p+i)->student[j].stusex);
        for(x=0;x<(*k);x++)
        {
          printf("%d ",(p+i)->student[j].achievement[x]);
        }
        printf("%f %f",(p+i)->student[j].ave,(p+i)->student[j].sum);
        flag=select_inquiry_caidan();
        switch(flag)
        {
          case 1:
          inquire_class(p,prept,pt,n,k,nz);
          case 2:
          inquire(p,prept,pt,n,k,nz);
          case 3:
          select_caidan(p,prept,pt,n,k,nz);
          case 4:
          {
            free(p);
            if(pt==prept)
            {
              free(prept);
            }
            else
            {
              free(prept);
              free(pt);
            }
            exit(0);
          }
        }
      }
    }
  }
  printf("未查询到相关信息\n");
  flag=select_inquiry_caidan();
  switch(flag)
  {
    case 1:
    inquire_class(p,prept,pt,n,k,nz);
    case 2:
    inquire(p,prept,pt,n,k,nz);
    case 3:
    select_caidan(p,prept,pt,n,k,nz);
    case 4:
    {
      free(p);
      if(pt==prept)
      {
        free(prept);
      }
      else
      {
        free(prept);
        free(pt);
      }
      exit(0);
    }
  }
}

int select_inquiry_caidan(void)
{
  int flag;
  while(1)
  {
    printf("请您选择操作\n");
    printf("1.继续查询\n2.返回选择查询类型功能\n3.返回主菜单\n4.退出系统");
    if(scanf("%d",&flag)!=1)
    {
      printf("请输入数字\n");
      while(getchar()!='\n');
      select_inquiry_caidan();
    }
    else if(flag<1||flag>4)
    {
      printf("无效选项\n");
      select_inquiry_caidan();
    }
    else
    break;
  }
return flag;
}

void inquire_stuid(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5])
{
  int i,j,x,flag;
  for(i=0;i<(*n);i++)
  {
    for(j=0;j<(p+i)->count;j++)
    {
      printf("%s ",(p+i)->student[j].stuid);
    }
    printf("\n");
  }
  char a[10];
  printf("请输入你所要查询的学号:");
  scanf("%3s",a);
  for(i=0;i<(*n);i++)
  {
    for(j=0;j<(p+i)->count;j++)
    {
      if(strcmp(a,(p+i)->student[j].stuid)==0)
      {
        printf("查询成功!\n");
        printf("%s %s %s %d %c ",(p+i)->classname,(p+i)->student[j].stuid,(p+i)->student[j].stuname,(p+i)->student[j].stuage,(p+i)->student[j].stusex);
        for(x=0;x<(*k);x++)
        {
          printf("%d ",(p+i)->student[j].achievement[x]);
        }
        printf("%f %f",(p+i)->student[j].ave,(p+i)->student[j].sum);
        flag=select_inquiry_caidan();
        switch(flag)
        {
          case 1:
          inquire_class(p,prept,pt,n,k,nz);
          case 2:
          inquire(p,prept,pt,n,k,nz);
          case 3:
          select_caidan(p,prept,pt,n,k,nz);
          case 4:
          {
            free(p);
            if(pt==prept)
            {
              free(prept);
            }
            else
            {
              free(prept);
              free(pt);
            }
            exit(0);
          }
        }
      }
    }
  }
  printf("未查询到相关信息\n");
  flag=select_inquiry_caidan();
  switch(flag)
  {
    case 1:
    inquire_class(p,prept,pt,n,k,nz);
    case 2:
    inquire(p,prept,pt,n,k,nz);
    case 3:
    select_caidan(p,prept,pt,n,k,nz);
    case 4:
    {
      free(p);
      if(pt==prept)
      {
        free(prept);
      }
      else
      {
        free(prept);
        free(pt);
      }
      exit(0);
    }
  }
}

void calculate_ave_sum(CLASS *p,int *n,int *k)
{
  printf("正在进行计算请耐心等待\n");
  int i,j,a;
  for(i=0;i<(*n);i++)
  {
    for(j=0;j<(p+i)->count;j++)
    {
      int sum=0;
      for(a=0;a<(*k);a++)
      {
        sum+=(p+i)->student[j].achievement[a];
      }
      (p+i)->student[j].sum=(float)sum;
      (p+i)->student[j].ave=((float)sum)/(*k);
    }
  }
  printf("计算完成结果已经记录到堆中\n");
}

void paixu(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5])
{
  int flag,flag1;
  end:printf("请选择排序方式\n");
  printf("1.按照平均分和总分排序\n2.返回上级菜单\n3.退出系统\n");
  if(scanf("%d",&flag)!=1)
  {
    printf("请输入数字\n");
    while(getchar()!='\n');
    goto end;
  }
  else if(flag<1||flag>3)
  {
    printf("无效选项\n");
    goto end;
  }
  else
  {
    switch(flag)
    {
      case 1:
      {
        end1:printf("请选择顺序1.逆序\n2.返回上级菜单\n3.退出系统\n4.返回主菜单\n");
        if(scanf("%d",&flag1)!=1)
        {
          printf("请输入数字\n");
          while(getchar()!='\n');
          goto end1;
        }
        else if(flag1<1||flag1>5)
        {
          printf("无效选项\n");
          goto end1;
        }
        else
        {
          switch(flag1)
          {
            case 1:pt=paixu_as_z(p,n,k,prept,pt);
            case 2:
            goto end1;
            case 3:
            {
              free(p);
              if(pt==prept)
              {
                free(prept);
              }
              else
              {
                free(prept);
                free(pt);
              }
              exit(0);
            }
            case 4:
            select_caidan(p,prept,pt,n,k,nz);
          }
        }
      }
      case 2:
      select_caidan(p,prept,pt,n,k,nz);
      case 3:
      {
        free(p);
        if(pt==prept)
        {
          free(prept);
        }
        else
        {
          free(prept);
          free(pt);
        }
        exit(0);
      }
    }
  }
}

void addname(CLASS *p,int *n)
{
  int i,j;
  for(i=0;i<(*n);i++)
  {
    for(j=0;j<(p+i)->count;j++)
    {
      strcpy((p+i)->student[j].classn,(p+i)->classname);
    }
  }
  printf("班级名已添加完毕可以进行排序\n");
}
STUDENT * paixu_as_z(CLASS *p,int *n,int *k,STUDENT *prept,STUDENT *pt)
{
  int i,j,a;
  STUDENT tmp;
  addname(p,n);
  printf("正在进行排序请稍后\n");
  for(i=0;i<(*n);i++)
  {
    for(j=0;j<(((p+i)->count)-1);j++)
    {
      for(a=1;a<(((p+i)->count)-j);a++)
      {
        if(((p+i)->student[a].ave)>((p+i)->student[a-1].ave))
        {
          tmp=(p+i)->student[a];
          (p+i)->student[a]=(p+i)->student[a-1];
          (p+i)->student[a-1]=tmp;
        }
      }
    }
  }
  printf("按照班级正序排序已完成正在进行年级排序\n");
  if(pt==prept)
  {
    STUDENT *fp;
    if((fp=(STUDENT *)malloc(400*sizeof(STUDENT)))!=NULL)
    {
      a=0;
      for(i=0;i<(*n);i++)
      {
        for(j=0;j<(p+i)->count;j++)
        {
          *(fp+a)=(p+i)->student[j];
          a++;
        }
      }
      int count=0;
      for(i=0;i<(*n);i++)
      {
        count+=(p+i)->count;
      }
      for(i=0;i<count-1;i++)
      {
        for(j=1;j<count-i;j++)
        {
          if(((fp+j)->ave)>((fp+j-1)->ave))
          {
            tmp=*(fp+j);
            *(fp+j)=*(fp+j-1);
            *(fp+j-1)=tmp;
          }
        }
      }
    }
    else
    {
      printf("申请空间失败!\n");
      free(p);
      free(prept);
      exit(-2);
    }
    return fp;
  }
  else
  {
    a=0;
    for(i=0;i<(*n);i++)
    {
      for(j=0;j<(p+i)->count;j++)
      {
        *(pt+a)=(p+i)->student[j];
        a++;
      }
    }
    int count=0;
    for(i=0;i<(*n);i++)
    {
      count+=(p+i)->count;
    }
    for(i=0;i<count-1;i++)
    {
      for(j=1;j<count-i;j++)
      {
        if(((pt+j)->ave)>((pt+j-1)->ave))
        {
          tmp=*(pt+j);
          *(pt+j)=*(pt+j-1);
          *(pt+j-1)=tmp;
        }
      }
    }
  }
  printf("按照成绩顺序排序已完成\n");
  return pt;
}
void printstu(CLASS *p,STUDENT *prept,STUDENT *pt,int *n,int *k,LEVEL nz[6][5])
{
  int i=0,j,a,pm=1,am=1,q;
  FILE *f=NULL;
  char c[15][20]={"班级","排名","学号","姓名","年龄","性别"};
  char z[12][20]={"最高分","最低分","平均分","优秀率","及格率"};
  if((f=fopen("D:\\code\\stug.txt","w+"))!=NULL)
  {
    printf("文件创建成功!正在进行读写操作\n");
    for(i=6;i<6+(*k);i++)
    {
      sprintf(c[i],"成绩%d",i-5);
    }
    strcpy(c[6+(*k)],"平均分");
    strcpy(c[7+(*k)],"总分");
    int length=8+(*k);
    for(i=5;i<5+(*k);i++)
    {
      sprintf(z[i],"成绩%d指标",i-4);
    }
    for(i=0;i<length;i++)
    {
      fprintf(f,"%s\t",c[i]);
    }
    fprintf(f,"\n");
    for(i=0;i<(*n);i++)
    {
      for(j=0;j<(p+i)->count;j++)
      {
        fprintf(f,"%s\t%d\t%s\t%s\t%d\t%c\t",(p+i)->classname,pm,(p+i)->student[j].stuid,(p+i)->student[j].stuname,(p+i)->student[j].stuage,(p+i)->student[j].stusex);
        for(a=0;a<(*k);a++)
        {
          fprintf(f,"%d\t",(p+i)->student[j].achievement[a]);
        }
        fprintf(f,"%f\t%f\n",(p+i)->student[j].ave,(p+i)->student[j].sum);
        pm++;
      }
      for(a=5;a<5+(*k);a++)
      {
        fprintf(f,"\t%s",z[a]);
      }
      fprintf(f,"\n");
      for(a=0;a<5;a++)
      {
        fprintf(f,"%s\t",z[a]);
        if(a<2)
        {
          for(q=0;q<(*k);q++)
          {
            fprintf(f,"%d\t",(p+i)->ab[q][a].fenshu);
          }
        }
        else if(a==2)
        {
          for(q=0;q<(*k);q++)
          {
            fprintf(f,"%f\t",(p+i)->ab[q][a].lv);
          }
        }
        else
        {
          for(q=0;q<(*k);q++)
          {
            fprintf(f,"%f%%\t",((p+i)->ab[q][a].lv)*100);
          }
        }
        fprintf(f,"\n");
      }
      pm=1;
      fprintf(f,"\n\n");
    }
    int co=0;
    for(i=0;i<(*n);i++)
    {
      co+=(p+i)->count;
    }
    for(i=0;i<length;i++)
    {
      fprintf(f,"%s\t",c[i]);
    }
    fprintf(f,"\n");
    for(j=0;j<co;j++)
    {
      fprintf(f,"%s\t%d\t%s\t%s\t%d\t%c\t",(pt+j)->classn,am,(pt+j)->stuid,(pt+j)->stuname,(pt+j)->stuage,(pt+j)->stusex);
      for(a=0;a<(*k);a++)
      {
        fprintf(f,"%d\t",(pt+j)->achievement[a]);
      }
      fprintf(f,"%f\t%f\n",(pt+j)->ave,(pt+j)->sum);
      am++;
    }
    for(i=5;i<5+(*k);i++)
    {
      fprintf(f,"\t%s",z[i]);
    }
    fprintf(f,"\n");
    for(a=0;a<5;a++)
    {
      fprintf(f,"%s\t",z[a]);
      if(a<2)
      {
        for(q=0;q<(*k);q++)
        {
          fprintf(f,"%d\t",nz[q][a].fenshu);
        }
      }
      else if(a==2)
      {
        for(q=0;q<(*k);q++)
        {
          fprintf(f,"%f\t",nz[q][a].lv);
        }
      }
      else
      {
        for(q=0;q<(*k);q++)
        {
          fprintf(f,"%f%%\t",(nz[q][a].lv)*100);
        }
      }
      fprintf(f,"\n");
    }
    fclose(f);
  }
  else
  {
    printf("文件创建或者打开失败!\n");
    free(p);
    if(prept==pt)
    {
      free(prept);
    }
    else
    {
      free(prept);
      free(pt);
    }
    exit(-4);
  }
}

void calculate_zhibiao(CLASS *p,STUDENT *pt,int *n,int *k,LEVEL nz[6][5])
{
  int i,j,a;
  for(i=0;i<(*n);i++)
  {
    int max[6],min[6],county[6]={0},countji[6]={0};
    float ave[6]={0},y[6],ji[6];
    for(j=0;j<(*k);j++)
    {
      for(a=0;a<(p+i)->count;a++)
      {
        ave[j]+=((p+i)->student[a].achievement[j])/(float)((p+i)->count);
        if(a==0)
        {
          max[j]=(p+i)->student[a].achievement[j];
          min[j]=(p+i)->student[a].achievement[j];
        }
        else
        {
          if(max[j]<(p+i)->student[a].achievement[j])
          {
            max[j]=(p+i)->student[a].achievement[j];
          }
          if(min[j]>(p+i)->student[a].achievement[j])
          {
            min[j]=(p+i)->student[a].achievement[j];
          }
        }
        if((p+i)->student[a].achievement[j]>=85)
        {
          county[j]++;
        }
        if((p+i)->student[a].achievement[j]>=60)
        {
          countji[j]++;
        }
      }
      y[j]=county[j]/(float)((p+i)->count);
      ji[j]=countji[j]/(float)((p+i)->count);
      (p+i)->ab[j][0].fenshu=max[j];
      (p+i)->ab[j][1].fenshu=min[j];
      (p+i)->ab[j][2].lv=ave[j];
      (p+i)->ab[j][3].lv=y[j];
      (p+i)->ab[j][4].lv=ji[j];
    }
  }

  int count=0;
  int max[6],min[6],county[6]={0},countji[6]={0};
  float ave[6]={0},y[6],ji[6];
  for(a=0;a<(*n);a++)
  {
    count+=(p+a)->count;
  }
  for(i=0;i<(*k);i++)
  {
    for(j=0;j<count;j++)
    {
      ave[i]+=(pt+j)->achievement[i]/(float)count;
      if(j==0)
      {
        max[i]=pt->achievement[i];
        min[i]=pt->achievement[i];
      }
      else
      {
        if(max[i]<(pt+j)->achievement[i])
        {
          max[i]=(pt+j)->achievement[i];
        }
        if(min[i]>(pt+j)->achievement[i])
        {
          min[i]=(pt+j)->achievement[i];
        }
      }
      if((pt+j)->achievement[i]>=85)
      {
        county[i]++;
      }
      if((pt+j)->achievement[i]>=60)
      {
        countji[i]++;
      }
    }
    y[i]=county[i]/(float)count;
    ji[i]=countji[i]/(float)count;
    nz[i][0].fenshu=max[i];
    nz[i][1].fenshu=min[i];
    nz[i][2].lv=ave[i];
    nz[i][3].lv=y[i];
    nz[i][4].lv=ji[i];
  }
}
  
  